package com.tsll.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tsll.dao.BookMapper;
import com.tsll.dao.PurchaseBookInfoMapper;
import com.tsll.entity.Book;
import com.tsll.entity.PurchaseBookInfo;
import com.tsll.service.PurchaseBookInfoService;
import com.tsll.vo.PurchaseBooksInfoVo;
import com.tsll.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/24/18:25
 */
@Service
public class PurchaseBookInfoServiceImpl implements PurchaseBookInfoService {


    @Autowired
    PurchaseBookInfoMapper purchaseBookInfoMapper;

    @Autowired
    BookMapper bookMapper;

    /**
     *
     * 批量添加申请采购图书信息
     * @param bookList
     * @param purchaseInfoId
     * @return
     */
    @Override
    public int addBatch(List<PurchaseBooksInfoVo> bookList, Integer purchaseInfoId) {
         return purchaseBookInfoMapper.addBatch(bookList,purchaseInfoId);

    }

    /**
     * 根据采购申请编号查询采购详情
     * @param purchaseinfoId 采购申请编号
     * @return
     */
    @Override
    public List<PurchaseBookInfo> selectByPurchaseinfoId(Integer purchaseinfoId) {
        return purchaseBookInfoMapper.selectByPurchaseinfoId(purchaseinfoId);
    }


    /**
     * 根据采购申请编号删除采购详情
     * @param purchaseinfoId
     * @return
     */
    @Override
    public int deleteByPurchaseinfoId(Integer purchaseinfoId){
        return purchaseBookInfoMapper.deleteByPurchaseinfoId(purchaseinfoId);
    }

    /**
     * 根据采购申请图书编号删除信息
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return purchaseBookInfoMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据id修改图书信息
     * @param purchaseBookInfo
     * @return
     */
    @Override
    public int updateById(PurchaseBookInfo purchaseBookInfo) {
        return purchaseBookInfoMapper.updateById(purchaseBookInfo);
    }

    @Override
    public int insetBookInfo(PurchaseBookInfo purchaseBookInfo) {
        return purchaseBookInfoMapper.insert(purchaseBookInfo);
    }




    @Override
    public Result check(Integer current, Integer pageSize, PurchaseBooksInfoVo purchaseBooksInfoVo) {
        //启动分页
        PageHelper.startPage(current, pageSize);
        List<PurchaseBookInfo> list = purchaseBookInfoMapper.check(purchaseBooksInfoVo);
        //封装到PgeInfo
        PageInfo<PurchaseBookInfo> pageInfo = new PageInfo<PurchaseBookInfo>(list);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public List<PurchaseBookInfo> Ware(PurchaseBooksInfoVo purchaseBooksInfoVo) {
        List<PurchaseBookInfo> list = purchaseBookInfoMapper.Ware(purchaseBooksInfoVo);
        //封装到PgeInfo
        PageInfo<PurchaseBookInfo> pageInfo = new PageInfo<PurchaseBookInfo>(list);
        return list;
    }

    @Override
    @Transactional
    public Result updatestatus(List<PurchaseBooksInfoVo> bookList) {
        List<Book> books = bookMapper.selectByVo(null);
        for (PurchaseBooksInfoVo purchaseBooksInfoVo : bookList) {
            for (Book book : books) {
                if (book.getBookName().equals(purchaseBooksInfoVo.getBooksname())) {
                    if (book.getBookPrice().equals(purchaseBooksInfoVo.getBookPrice())) {
                        purchaseBooksInfoVo.setBookCount(purchaseBooksInfoVo.getBookCount() + book.getBookCount());
                        System.out.println(purchaseBooksInfoVo.getBookCount());
                        bookMapper.updateBookCountById(book.getBookId(), purchaseBooksInfoVo.getBookCount());
                        purchaseBookInfoMapper.updatestatus(purchaseBooksInfoVo.getId());
                        break;
                    }

                }
            }
            bookMapper.addBatch(purchaseBooksInfoVo);
            purchaseBookInfoMapper.updatestatus(purchaseBooksInfoVo.getId());

        }
        return  new Result(200,"入库成功",null);
    }


}
